package com.budwk.app.sqfwV2.component.centuryCity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskPdfDTO;
import com.budwk.app.sqfwV2.enums.CenturyCitySafeServiceEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

/**
 * @author zhangp
 * @description TODO
 * @className CommRowDataBuild
 * @Date 12:44 2024/2/20
 */
public class CommCenturyCityDataHandleUtil {

    public static void rowDataBuild(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {
        Sqfw_formSummary_new sqfwFormSummaryNew = forSummaryBO.getSqfwFormSummaryNew();
        SafeCheckTaskPdfDTO safeCheckTaskPdfDTO = JSONObject.parseObject(sqfwFormSummaryNew.getParamData(), SafeCheckTaskPdfDTO.class);
        List<SafeCheckTaskParamDataDTO> safeCheckTaskParamDataDTOList = safeCheckTaskPdfDTO.getHiddenDanger();

        if(StrUtil.isNotBlank(safeCheckTaskPdfDTO.getVisitPicturesFileList())){
            List<String>  list = Arrays.asList(safeCheckTaskPdfDTO.getVisitPicturesFileList().split(","));
            rowDataMap.put("visitPicturesFileList", addPictureToByteList(list));
        }else {
            rowDataMap.put("visitPicturesFileList", null);
        }
        rowDataMap.put("fillDateStr", DateUtil.format(sqfwFormSummaryNew.getFillDate(), "yyyy年MM月dd日"));
        rowDataMap.put("community", sqfwFormSummaryNew.getCommunity());
        rowDataMap.put("county", sqfwFormSummaryNew.getCounty());
        rowDataMap.put("rectificationMeasure", safeCheckTaskParamDataDTOList.get(0).getRectificationMeasure());
        rowDataMap.put("nineSmallPlacesIssuesAndRisks", safeCheckTaskParamDataDTOList.get(0).getNineSmallPlacesIssuesAndRisks());
        rowDataMap.put("riskLevel", safeCheckTaskParamDataDTOList.get(0).getRiskLevel());
        rowDataMap.put("reportNineSmallPlaces", safeCheckTaskParamDataDTOList.get(0).getReportNineSmallPlaces());
        rowDataMap.put("dispositionNineSmallPlaces", safeCheckTaskParamDataDTOList.get(0).getDispositionNineSmallPlaces());
        rowDataMap.put("riskImages", addPictureToByteList(safeCheckTaskParamDataDTOList.get(0).getRiskImages(), 1));
        rowDataMap.put("rectificationBackPhoto", addPictureToByteList(safeCheckTaskParamDataDTOList.get(0).getRectificationBackPhoto(), 1));
        rowDataMap.put("inspectionPerson", sqfwFormSummaryNew.getCreateUserName());
        if(cn.hutool.core.util.StrUtil.isEmpty(safeCheckTaskPdfDTO.getPeers())){
            safeCheckTaskPdfDTO.setPeers( sqfwFormSummaryNew.getCreateUserName());
        }
        rowDataMap.put("peers", safeCheckTaskPdfDTO.getPeers());
    }

    public static void buildUnitName(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        BaseExcelParamDTO baseExcelParamDTO = excelCreateParam.getBaseExcelParamDTO();
        if (baseExcelParamDTO.getUnitName().contains(CommonConstants.DIRECTLY_UNDER)) {
            String unitName = baseExcelParamDTO.getProvince() + baseExcelParamDTO.getCity() + baseExcelParamDTO.getDistrict();
            if (!CommonConstants.DIRECTLY_UNDER.equals(baseExcelParamDTO.getCounty())) {
                unitName += baseExcelParamDTO.getCounty();
            }
            if (!CommonConstants.DIRECTLY_UNDER.equals(baseExcelParamDTO.getCommunity())) {
                unitName += baseExcelParamDTO.getCommunity();
            }
            excelParam.put("unitName", unitName);
        }
        if (baseExcelParamDTO.getCounty().equals(CommonConstants.DIRECTLY_UNDER)) {
            excelParam.put("county", "");
        }

    }

    public static boolean checkData(List<Sqfw_formSummary_new> sqfwFormSummaryNewList) {
        boolean flag = false;
        List<String> typeList = sqfwFormSummaryNewList.stream().map(m -> JSONObject.parseObject(m.getParamData(), SafeCheckTaskPdfDTO.class).getType()).distinct().collect(Collectors.toList());

        for (CenturyCitySafeServiceEnum item : CenturyCitySafeServiceEnum.values()) {
            if (typeList.contains(item.getType())) {
                flag = true;
                break;
            }

        }
        return flag;
    }

}
